package top.huhuiyu.teachproject.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;

@Data
public class PageBean implements Serializable {

  private static final long serialVersionUID = 1L;
  public static final long MIN_SIZE = 1;
  public static final long MAX_SIZE = 1000;
  public static final int DEFAULT_PAGE = 1;
  public static final int DEFAULT_SIZE = 10;

  private long pageNumber = DEFAULT_PAGE;
  private long pageSize = DEFAULT_SIZE;
  private long total = 0;
  private long pageCount = 0;

  public void setPageNumber(long pageNumber) {
    if (pageNumber >= DEFAULT_PAGE) {
      this.pageNumber = pageNumber;
    }
  }

  public void setPageSize(long pageSize) {
    if (pageSize < MIN_SIZE || pageSize > MAX_SIZE) {
      return;
    }
    this.pageSize = pageSize;
  }

  public PageBean buildPageInfo(IPage<?> page) {
    if (page == null) {
      return this;
    }
    this.setTotal(page.getTotal());
    this.setPageCount(page.getPages());
    return this;
  }

  public <T> IPage<T> toPage(Class<T> clazz) {
    return new Page<>(this.pageNumber, this.pageSize);
  }

}
